class Solution:
    def romanToInt(self, s: str) -> int:
        # 构建字典,包含所有特殊情况
        d = {'I': 1, 'IV': 3, 'V': 5, 'IX': 8, 'X': 10,
             'XL': 30, 'L': 50, 'XC': 80, 'C': 100,
             'CD': 300, 'D': 500, 'CM': 800, 'M': 1000}
        # 求和
        # 将字符串转换为字典
        # str_dict = dict(enumerate(s))
        # print(str_dict)
        # 从左到右,两两相加

        return sum(d.get(s[max(i - 1, 0):i + 1], d[n]) for i, n in enumerate(s))


luoma1 = "III"  # 3
luoma2 = "IV"  # 4
luoma3 = "IX"  # 9
luoma4 = "LVIII"  # 58
luoma5 = "MCMXCIV"  # 1994

# 测试
solution = Solution()
print(solution.romanToInt(luoma1))
print(solution.romanToInt(luoma2))
print(solution.romanToInt(luoma3))
print(solution.romanToInt(luoma4))
print(solution.romanToInt(luoma5))
